/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controlPlaylisted;


import dataPlaylisted.User;
import dataPlaylisted.Listener;

import guiPlaylisted.view.UserView;

import guiPlaylisted.PlaylistedApp;

import Response.ResponseUser;
import Request.RequestUser;

import dataBasePlaylisted.setUserListCommand;
import dataBasePlaylisted.getUserListCommand;
import dataPlaylisted.Playlist;


import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;


/**
 *
 * @author Administrador
 */
public class UserController {

    private User m_user;

    private boolean m_logged;

    private ArrayList<User> m_users;

    private UserView m_userView;

    private PlaylistController m_playlistCtrl;

    private static UserController instance;

    private HashMap<String, ControlUserCommand> m_mapCommand;

    private int free_id;

    public UserController() throws FileNotFoundException, IOException, ClassNotFoundException{
        m_user = new Listener();
        m_users = new ArrayList<User>();
        m_mapCommand = new HashMap();
        free_id = 1;
        this.initCommand();
    }

    public static UserController getInstance() throws FileNotFoundException, IOException, ClassNotFoundException{
        instance = PlaylistedApp.getUserCtrl();
        if(instance == null)
            instance = new UserController();
        return instance;
    }

    private void setUser(User _user){
        m_user = _user;
        m_userView.setUser();
    }

    public User getUser(){
        return m_user;
    }

    public void setUserView() throws FileNotFoundException, IOException, ClassNotFoundException{
        m_userView = UserView.getInstance();
    }

    public void setPlaylistCtrl() throws FileNotFoundException, IOException, ClassNotFoundException{
        m_playlistCtrl = PlaylistController.getInstance();
    }

    private UserView getUserView(){
        return m_userView;
    }

    public void loadUserList() throws FileNotFoundException, IOException, ClassNotFoundException{

        RequestUser req;
        ResponseUser resp;

        req = new RequestUser();
        resp = new ResponseUser();

        req.setParameter("loadUserList");

        this.doPost(req, resp);

        if(resp.getObjectList() != null){
            m_users = resp.getObjectList();
            free_id = m_users.size() + 1;
        }
    }

    private void saveUserList() throws FileNotFoundException, IOException, ClassNotFoundException{

        RequestUser req;
        ResponseUser resp;

        req = new RequestUser();
        resp = new ResponseUser();

        req.setParameter("saveUserList");
        req.setObjectList(m_users);

        this.doPost(req, resp);
    }

    public void setUserOnList(String _login, String _pass, String _userName) throws IOException, ClassNotFoundException{

        User _user;
        _user = new Listener();
        _user.setId(free_id);
        free_id++;
        _user.setUserLogin(_login);
        _user.setUserPass(_pass);
        _user.setUserName(_userName);

        int size = m_users.size();
        for (int i =0; i < size; i++){
            if(_user.getUserLogin().equals(m_users.get(i).getUserLogin()))
                if(_user.getUserPass().equals(m_users.get(i).getUserPass()))
                    //usuário já existe
                    return;
        }

        m_users.add(_user);

        this.saveUserList();
    }

    public void login(String _login, String _pass) throws ClassNotFoundException{
        
        User user_login;
        user_login = new Listener();

        user_login.setUserLogin(_login);
        user_login.setUserPass(_pass);

        RequestUser req;
        ResponseUser resp;

        req = new RequestUser();
        resp = new ResponseUser();

        ArrayList<User> _users;
        _users = new ArrayList<User>();

        _users.addAll(m_users);
        _users.add(user_login);

        req.setParameter("login");
        req.setObjectList(_users);

        this.doPost(req, resp);

        if (resp.getObjectList().get(0) != null){
            this.setUser(resp.getObjectList().get(0));
            this.setPlaylistList();
            m_logged = true;
        }
    }

    public boolean getLogged(){
            return m_logged;
        }

    private void initCommand() throws FileNotFoundException, IOException, ClassNotFoundException{

        m_mapCommand.put("login", new loginCommand());
        m_mapCommand.put("loadUserList", new getUserListCommand());
        m_mapCommand.put("saveUserList", new setUserListCommand());
    }

    private void doPost(RequestUser req, ResponseUser resp) throws ClassNotFoundException{
        m_mapCommand.get(req.getParameter()).execute(req, resp);
    }

    public void setPlaylistList(){
        ArrayList<Playlist> _playlistList;
        _playlistList = new ArrayList<Playlist>() ;

        Playlist _playlist;
        _playlist = new Playlist();

        for(int i=0; i < m_playlistCtrl.getPlaylistList().size(); i++){
            if (m_playlistCtrl.getPlaylistList().get(i).getUserId() == m_user.getId()){
                   _playlist = m_playlistCtrl.getPlaylistList().get(i);
                   _playlistList.add(_playlist);
            }
        }
        m_user.setPlaylistList(_playlistList);
    }
}
